package br.com.mundodaprata.store.data.gen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import br.com.mundodaprata.store.data.jdbc.StreetRepositoryJDBC;
import br.com.mundodaprata.store.model.City;
import br.com.mundodaprata.store.model.Neighborhood;
import br.com.mundodaprata.store.model.Street;

public class StreetGen implements DataGen {
   
   @Override
   public void gen() throws Exception {
      List<Street> streets = new ArrayList<Street>();
      
      File file = new File("C:\\java\\streets.txt");
      FileReader fr = new FileReader(file);
      BufferedReader reader = new BufferedReader(fr);
      String line;
      
      while ((line = reader.readLine()) != null) {
         String[] data = line.split(";");
         
         Street street = new Street();
         street.setCode(Long.parseLong(data[0]));
         street.setState(data[1]);
         
         City city = new City();
         city.setCode(Long.parseLong(data[2]));
         
         street.setCity(city);
         
         Neighborhood neighborhood = new Neighborhood();
         neighborhood.setCode(Long.parseLong(data[3]));
         
         street.setNeighborhood(neighborhood);
         
         street.setName(data[4]);
         street.setZipCode(data[5]);
         street.setType(data[6]);
         
         streets.add(street);
      }
      
      StreetRepositoryJDBC jdbc = new StreetRepositoryJDBC();
      jdbc.insert((Street[]) streets.toArray(new Street[streets.size()]));
      jdbc.closeConnection();
   }
}